package library.http;

import java.io.File;

import library.http.progress.HttpProgressHelper;
import library.http.progress.HttpUploadListener;
import library.http.progress.ProgressPublisher;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.ByteString;

class FilePartWrapper<T> {
    private String paramName;
    private String fileName;
    private MediaType mediaType;
    private T originData;
    private T targetData;
    private int byteOffset = -1;
    private int byteCount = -1;
    private HttpUploadListener progress;
    private long rateProgress;
    private long rateSpeed;

    private FilePartWrapper() {

    }

    public String fileName() {
        return fileName;
    }

    public static <T> FilePartWrapper<T> create(String fileName, T Data, MediaType mediaType) {
        return create(fileName, Data, Data, mediaType, -1, -1);
    }

    public static <T> FilePartWrapper<T> create(String fileName, T originData, T targetData, MediaType mediaType) {
        return create(fileName, originData, targetData, mediaType, -1, -1);
    }

    public static <T> FilePartWrapper<T> create(String fileName, T Data, MediaType mediaType, int byteOffset, int byteCount) {
        return create(fileName, Data, Data, mediaType, byteOffset, byteCount);
    }

    public static <T> FilePartWrapper<T> create(String fileName, T originData, T targetData, MediaType mediaType, int byteOffset, int byteCount) {
        FilePartWrapper wrapper = new FilePartWrapper();
        wrapper.fileName = fileName;
        wrapper.mediaType = mediaType;
        wrapper.originData = originData;
        wrapper.targetData = targetData;
        wrapper.byteOffset = byteOffset;
        wrapper.byteCount = byteCount;
        return (FilePartWrapper<T>) wrapper;
    }

    public <T> FilePartWrapper<T> paramName(String paramName) {
        this.paramName = paramName;
        return (FilePartWrapper<T>) this;
    }

    public String getParamName() {
        return paramName;
    }

    public void onProgress(HttpUploadListener<T> progress, long rateProgress, long rateSpeed) {
        this.progress = progress;
        this.rateProgress = rateProgress;
        this.rateSpeed = rateSpeed;
    }

    public RequestBody body() {
        RequestBody fileBody;
        if (targetData instanceof File) {
            fileBody = RequestBody.create(mediaType, (File) targetData);
        } else if (targetData instanceof ByteString) {
            fileBody = RequestBody.create(mediaType, (ByteString) targetData);
        } else if (targetData instanceof byte[]) {
            if (byteOffset == -1 || byteCount == -1) {
                fileBody = RequestBody.create(mediaType, (byte[]) targetData);
            } else {
                fileBody = RequestBody.create(mediaType, (byte[]) targetData, byteOffset, byteCount);
            }
        } else {
            fileBody = RequestBody.create(mediaType, String.valueOf(targetData));
        }
        if (progress != null) {
            return HttpProgressHelper.addProgressUp(fileBody, new ProgressPublisher(originData, (HttpUploadListener<T>) progress, 0, rateProgress, rateSpeed));
        } else
            return fileBody;
    }
}

